/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.mps.service.spring;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.redragon.commons.lang.Times;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.hr.dao.model.HrStaffInfoRO;
import com.erp.hr.service.HrCommonService;
import com.erp.masterdata.common.service.MasterDataCommonService;
import com.erp.masterdata.material.dao.model.MdMaterial;
import com.erp.order.so.dao.model.SoHead;
import com.erp.order.so.dao.model.SoLine;
import com.erp.order.so.dao.model.SoLineCO;
import com.erp.order.so.service.SoHeadService;
import com.erp.order.so.service.SoLineService;
import com.erp.prod.mps.dao.ProdMpsHeadDao;
import com.erp.prod.mps.dao.ProdMpsLineDao;
import com.erp.prod.mps.dao.model.ProdMpsHead;
import com.erp.prod.mps.dao.model.ProdMpsHeadCO;
import com.erp.prod.mps.dao.model.ProdMpsLine;
import com.erp.prod.mps.dao.model.ProdMspSoR;
import com.erp.prod.mps.service.ProdMpsHeadService;
import com.erp.prod.mps.service.ProdMpsLineService;
import com.erp.prod.mps.service.ProdMspSoRService;

@Service
@Transactional(rollbackFor=Exception.class)
public class ProdMpsHeadServiceImpl implements ProdMpsHeadService {

    //注入Dao
    @Autowired
    private ProdMpsHeadDao prodMpsHeadDao;
    @Autowired
    private ProdMpsLineService prodMpsLineService;
    @Autowired
    private ProdMpsLineDao prodMpsLineDao;
    @Autowired
    private SoHeadService soHeadService;
    @Autowired
    private SoLineService soLineService;
    @Autowired
    private HrCommonService hrCommonService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private ProdMspSoRService prodMspSoRService;
    
    @Override
    public void insertDataObject(ProdMpsHead obj) {
        this.prodMpsHeadDao.insertDataObject(obj);
    }

    @Override
    public void updateDataObject(ProdMpsHead obj) {
        this.prodMpsHeadDao.updateDataObject(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(ProdMpsHead obj) {
        this.prodMpsHeadDao.insertOrUpdateDataObject(obj);
    }

    @Override
    public void deleteDataObject(ProdMpsHead obj) {
        this.prodMpsHeadDao.deleteDataObject(obj);
        this.prodMpsLineService.deleteProdMpsLineByMpsHeadCode(obj.getMpsHeadCode());
        this.prodMspSoRService.deleteRelateByMpsHeadCode(obj.getMpsHeadCode());
    }

    @Override
    public List<ProdMpsHead> getDataObjects() {
        return this.prodMpsHeadDao.getDataObjects();
    }

    @Override
    public ProdMpsHead getDataObject(int id) {
        return this.prodMpsHeadDao.getDataObject(id);
    }

    @Override
    public ProdMpsHead getDataObject(String code) {
        return this.prodMpsHeadDao.getDataObject(code);
    }

    @Override
    public List<ProdMpsHead> getDataObjects(ProdMpsHeadCO paramObj) {
        return this.prodMpsHeadDao.getDataObjects(paramObj);
    }

    @Override
    public List<ProdMpsHead> getDataObjects(Pages pages) {
        return this.prodMpsHeadDao.getDataObjects(pages);
    }
    
    @Override
    public List<ProdMpsHead> getDataObjects(Pages pages, ProdMpsHeadCO paramObj) {
        return this.prodMpsHeadDao.getDataObjects(pages, paramObj);
    }
    
    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, ProdMpsHeadCO paramObj) {
        return this.prodMpsHeadDao.getDataObjectsArray(pages, paramObj);
    }
    
    @Override
    public List<ProdMpsHead> getDataObjectsForDataAuth(String dataAuthSQL, Pages pages, ProdMpsHeadCO paramObj) {
        return this.prodMpsHeadDao.getDataObjectsForDataAuth(dataAuthSQL, pages, paramObj);
    }
    
    @Override
    public void updateApproveStatus(String code, String approveStatus) {
        this.prodMpsHeadDao.updateApproveStatus(code, approveStatus);
        
        if(approveStatus.equals("UNSUBMIT")) {
            //更新行表版本号
            this.prodMpsLineDao.updateLineForVersion(code);
            
        }
    }
    
    @Override
    public List<ProdMpsHead> getMpsListForMrp(Pages pages, ProdMpsHeadCO prodMpsHeadCO) {
        return this.prodMpsHeadDao.getMpsListForMrp(pages, prodMpsHeadCO);
    }
    
    @Override
    public String autoCreateMps(String billHeadCode) {
        //创建MPS头
        ProdMpsHead prodMpsHead = new ProdMpsHead();
        //设置职员信息
        HrStaffInfoRO staffInfo = this.hrCommonService.getStaffInfo(ShiroUtil.getPrincipal());
        prodMpsHead.setStaffCode(staffInfo.getStaffCode());
        prodMpsHead.setDepartmentCode(staffInfo.getDepartmentCode());
        //时间
        Times time = new Times();
        prodMpsHead.setMpsHeadCode(time.getDate()+"_"+SnowFlake.getConcurrentId().toString()+"_MPS");
        prodMpsHead.setMpsName(time.getDate()+"-主生产计划");
        //插入mps头
        this.prodMpsHeadDao.insertDataObject(prodMpsHead);
        
        //格式化so编码
        String[] billHeadCodeArray = billHeadCode.split(":");
        for(String billHeadCodeTemp: billHeadCodeArray) {
            if(StringUtils.isNotBlank(billHeadCodeTemp)) {
                //获取so头
                SoHead soHead = this.soHeadService.getDataObject(billHeadCodeTemp);
                //获取so行
                Pages pages = new Pages();
                pages.setPage(1);
                pages.setMax(100);
                SoLineCO soLineCO = new SoLineCO();
                soLineCO.setSoHeadCode(billHeadCodeTemp);
                List<SoLine> soLineList = this.soLineService.getSoLineListBySoHeadCode(pages, soLineCO);
                
                for(SoLine soLine: soLineList) {
                    //获取物料数据
                    MdMaterial mdMaterial = this.masterDataCommonService.getMdMaterialInfoCache(soLine.getMaterialCode());
                    
                    //创建MPS头
                    ProdMpsLine prodMpsLine = new ProdMpsLine();
                    prodMpsLine.setBomProperty(mdMaterial.getBomProperty());
                    prodMpsLine.setMaterialCode(soLine.getMaterialCode());
                    prodMpsLine.setMpsHeadCode(prodMpsHead.getMpsHeadCode());
                    prodMpsLine.setMpsLineCode(SnowFlake.getConcurrentId().toString());
                    prodMpsLine.setPlanStartDate(soHead.getStartDate()==null?time.getJavaDate():soHead.getStartDate());
                    prodMpsLine.setPlanEndDate(soHead.getEndDate()==null?time.getJavaDate():soHead.getEndDate());
                    prodMpsLine.setPlanQuantity(soLine.getQuantity());
                    //插入mps行
                    this.prodMpsLineDao.insertDataObject(prodMpsLine);
                    //插入mps与so关联表
                    ProdMspSoR prodMspSoR = new ProdMspSoR();
                    prodMspSoR.setMpsHeadCode(prodMpsLine.getMpsHeadCode());
                    prodMspSoR.setMpsLineCode(prodMpsLine.getMpsLineCode());
                    prodMspSoR.setSoHeadCode(soLine.getSoHeadCode());
                    prodMspSoR.setSoLineCode(soLine.getSoLineCode());
                    this.prodMspSoRService.insertDataObject(prodMspSoR);
                }
            }
        }
        
        //返回json数据
        return "{\"errCode\":0, \"errMsg\": \"\", \"mpsHeadId\": "+prodMpsHead.getMpsHeadId()+", \"mpsHeadCode\": \""+prodMpsHead.getMpsHeadCode()+"\"}";
    }
    
}